package cn.rjzjh.tapestry.tams.pages.activiti;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.util.TextStreamResponse;

import cn.rjzjh.commons.util.apiext.StringUtil;
import cn.rjzjh.commons.util.callback.IConvertValue;
import cn.rjzjh.commons.util.web.EasyUiAssist;
import cn.rjzjh.tapestry.component.SymbolConstantsCus;
import cn.rjzjh.tapestry.component.services.ITools;
import cn.rjzjh.tapestry.component.tools.TapestryAssist;
import cn.rjzjh.tapestry.tams.base.ParentPage;

/****
 * 流程配置页面
 * 
 * @author Administrator
 *
 */
public class ConfManager extends ParentPage {

	@Inject
	private RepositoryService repositoryService;

	@Inject
	private ITools tools;

	@Inject
	@Symbol(SymbolConstantsCus.PATH_FILE_UPLOAD_DIR)
	private String defaultUploadDir;// 默认的文件上传目录,在service里配置

	public TextStreamResponse onQuery() {
		JSONObject params = TapestryAssist.packParams(request);
		String cfName = TapestryAssist.getParam(params, "cfName");
		ProcessDefinitionQuery query = repositoryService
				.createProcessDefinitionQuery();
		if (StringUtil.isNotNull(cfName)) {
			query.processDefinitionNameLike(cfName);
		}
		List<ProcessDefinition> processDefinitionList = query.list();
		Map<String, IConvertValue> conv = new HashMap<String, IConvertValue>();
		String str = EasyUiAssist.getJsonForGrid(processDefinitionList,
				new String[] { "id", "deploymentId", "name" }, conv,
				processDefinitionList.size());
		return retStream(str);
	}

	public void onUploadProcess() {
		List<File> uploadlist = tools.saveUpload();
		DeploymentBuilder deploymentBuilder = repositoryService
				.createDeployment();
		try {
			for (File file : uploadlist) {
				deploymentBuilder.addInputStream(file.getName(),
						new FileInputStream(file));
				deploymentBuilder.deploy();
			}
		} catch (Exception e) {
		}

	}
}
